//启动页面时 vue渲染页面
var vm = new Vue({
    watch:{
        immediate:true,
        deep:true,
        deptInfo:function(){
            this.$nextTick(function(){
                $(".forminfo>li:nth-child(7)>select>option").each(function () {
                    var a = $(this).attr("power");
                    if(a==vm.message.deptId){
                        $(this).attr("selected",true);
                    }
                })
            })
        },
        roleInfo:function(){
            this.$nextTick(function(){
                $(".forminfo>li:nth-child(6)>select>option").each(function () {
                    var a = $(this).attr("power");
                    if(a==vm.message.deptId){
                        $(this).attr("selected",true);
                    }
                })
            })
        },
        positionInfo:function(){
            this.$nextTick(function(){
                $(".forminfo>li:nth-child(12)>select>option").each(function () {
                    var a = $(this).attr("power");
                    if(a==vm.message.deptId){
                        $(this).attr("selected",true);
                    }
                })
            })
        }
    },
    el:"#app",
    data:{
      message: {
          deptId:'',
          roleId:'',
          positionid:''
      },
      powerflag:true,
      sexflag:true,
      deptInfo:'',
      roleInfo:'',
      positionInfo:''
    },
    methods:{
        //渲染用户信息
        getStaffInfo:function () {
            var that = this.message;
            axios({
                method:"post",
                url:"http://localhost:8099/PersonalInfo/getSelfInfo",
                // params:{
                //     "id":1
                // }
            }).then(
                function(result){
                    vm.message=result.data;
                    console.log(result.data);
                    if(result.data.roleId===3){
                        vm.powerflag=false;
                    }
                    if(result.data.sex===1){
                        vm.sexflag=false;
                    }
                    console.log(vm.powerflag);
                },function (err) {
                    console.log(err);
                }
            )
        },
        //获取部门信息
        getDeptInfo:function(){
            axios.post("http://localhost:8099/PersonalInfoQuery/getAllDept").then(
                function(result){
                    vm.deptInfo=result.data;
                    console.log(vm.deptInfo);
                },function (err) {
                    console.log(err);
                }
            )
        },
        //获取角色信息
        getRoleInfo:function(){
            axios({
                method:"post",
                url:"http://localhost:8099/role/queryRolesByCondition"
            }).then(
                function(result){
                    vm.roleInfo=result.data;
                    console.log(vm.roleInfo);
                },function (err) {
                    console.log(err);
                }
            )
        },
        //获取职位信息
        getPositionInfo:function(){
            axios({
                method:"post",
                url:"http://localhost:8099/staff/queryPositionList"
            }).then(
                function(result){
                    vm.positionInfo=result.data;
                    console.log(vm.positionInfo);
                },function (err) {
                    console.log(err);
                }
            )
        },
        //打开提示框
        changePassword:function () {
            $("#tip1").fadeIn(200)
        },
        //关闭提示框(修改时)
        closetip1:function(){
            var changingPwd = $("#changingPwd").val();
            changingPwd = changingPwd.trim();
            if(changingPwd===""){
                alert("新密码不能为空！");
            }else{
                axios.post("http://localhost:8099/PersonalInfo/staffPwdUpdate",{password:changingPwd}).then(
                    function(result){
                        if(result){
                            alert("密码修改成功！")
                        }else{
                            alert("密码修改失败！")
                        }
                    }
                )
            }
            $("#tip1").fadeOut(200);
        },
        //关闭提示框(没有修改时)
        closetip2:function(){
            $("#tip1").fadeOut(200);
        }
    },
    //页面加载完自动执行
    mounted(){
        this.getStaffInfo();
        this.getDeptInfo();
        this.getRoleInfo();
        this.getPositionInfo();
    }
})


//改变公开或者不公开的状态
function changeState(n) {
    var state = $(n).val();
    var flag = $(n).prevAll("label").html();
    if(state==="公开"){
        $(n).val("不公开");
    }else if(state==="不公开"){
        $(n).val("公开");
    }
}


//提交修改
function submit(){
    var flag='110';
    var staffId = $(".forminfo").children("li:nth-child(1)").children("input").val();
    var name = $(".forminfo").children("li:nth-child(2)").children("input").val();
    var sex = $(".forminfo").children("li:nth-child(4)").children("input:checked").val();
    var age = $(".forminfo").children("li:nth-child(5)").children("input").val();
    var positionid = $(".forminfo>li:nth-child(6)>select>option:checked").attr("power");
    var deptId = $(".forminfo>li:nth-child(7)>select>option:checked").attr("power");
    var tel = $(".forminfo").children("li:nth-child(8)").children("input").val();
    var email = $(".forminfo").children("li:nth-child(9)").children("input").val();
    var hobby = $(".forminfo").children("li:nth-child(10)").children("textArea").val();
    var selfPartiality = $(".forminfo").children("li:nth-child(11)").children("textArea").val();
    var roleId = $(".forminfo>li:nth-child(12)>select>option:checked").attr("power");

    alert(positionid+" "+deptId+" "+roleId)

    var flag1=$(".forminfo").children("li:nth-child(4)").children("input:last-child").val();
    var flag2=$(".forminfo").children("li:nth-child(5)").children("input:last-child").val();
    var flag3=$(".forminfo").children("li:nth-child(8)").children("input:last-child").val();
    var flag4=$(".forminfo").children("li:nth-child(9)").children("input:last-child").val();
    var flag5=$(".forminfo").children("li:nth-child(10)").children("input:last-child").val();
    var flag6=$(".forminfo").children("li:nth-child(11)").children("input:last-child").val();
    flag1=staticchangetoInteger(flag1);
    flag2=staticchangetoInteger(flag2);
    flag3=staticchangetoInteger(flag3);
    flag4=staticchangetoInteger(flag4);
    flag5=staticchangetoInteger(flag5);
    flag6=staticchangetoInteger(flag6);
    flag+=flag1+flag2+'11'+flag3+flag4+flag5+flag6+'1';
    var telzhenzeflag =/^1[3-9]{1}[0-9]{9}$/;
    var emailzhenzeflag =/^([a-zA-Z0-9_\-\.])+\@([a-zA-Z0-9_\-\.])+\.([a-zA-Z]{2,8})$/;


    // checkTel(tel).then(n=>{
    //     checkEmail(email).then(n1=>{
    //         checkStaffId(staffId).then(n2=>{
    //             if(telzhenzeflag.test(tel)&&emailzhenzeflag.test(email)){
    //                 if(n&&n1&&n2){
    //                     axios.post(
    //                         "http://localhost:8099/PersonalInfo/staffUpdate",
    //                         {
    //                             staffId:staffId,
    //                             name:name,
    //                             sex:sex,
    //                             age:age,
    //                             positionid:positionid,
    //                             deptId:deptId,
    //                             tel:tel,
    //                             email:email,
    //                             hobby:hobby,
    //                             selfPartiality:selfPartiality,
    //                             roleId:roleId,
    //                             flag:flag
    //                         }
    //                     ).then(
    //                         function(result){
    //                             if(result.data==true){
    //                                 alert("员工信息更改成功！");
    //                             }else if(result.data==false){
    //                                 alert("员工信息更改失败！");
    //                             }
    //                         },function (err) {
    //                             console.log(err);
    //                         }
    //                     )
    //                 }
    //             }else{
    //                 alert("联系方式填写格式错误！请确认！");
    //             }
    //         })
    //     })
    // })

}

//根据公开或者不公开的状态确定公开码
function staticchangetoInteger(n){
    if(n==="公开"){
       return '1';
    }else if(n==="不公开"){
       return '0';
    }
}

function checkEmail(n) {
    return axios.post(
        "http://localhost:8099/PersonalInfo/checkEmail",
        {email:n}
    ).then(
        function(result){
            console.log(result.data);
            if(result.data!=""){
                alert("邮箱已存在!")
                return false;
            }else {
                return true;
            }
        },function (err) {
            console.log(err);
        }
    )
}

function checkTel(n) {
    return axios.post(
        "http://localhost:8099/PersonalInfo/checkTel",
        {tel:n}
    ).then(
        function(result){
            if(result.data!=""){
                alert("电话号码已存在！");
                return false;
            }else {
                return true;
            }
        },function (err) {
            console.log(err);
        }
    )
}

function checkStaffId(n) {
    return axios.post(
        "http://localhost:8099/PersonalInfo/checkStaffId",
        {staffId:n}
    ).then(
        function(result){
            if(result.data!=""){
                alert("员工号存在！");
                return false;
            }else {
                return true;
            }
        },function (err) {
            console.log(err);
        }
    )
}


